﻿@page "/movies"
@rendermode InteractiveServer

@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.QuickGrid
@using BlazorWebAppMovies.Models
@using BlazorWebAppMovies.Data
@implements IAsyncDisposable
@inject IDbContextFactory<BlazorWebAppMovies.Data.BlazorWebAppMoviesContext> DbFactory

<PageTitle>Index</PageTitle>

<h1>Index</h1>
<div>
    @* data-enhance 特性将增强型导航   保留搜索的值，请添加 data-permanent 实际是get请求*@
@*     <form action="/movies" data-enhance data-permanent>
        <input type="search" name="titleFilter" />
        <input type="submit" value="Search" />
    </form> *@

    <input type="search" @bind="titleFilter" @bind:event="onchange" />
</div>
<p>
    <a href="movies/create">Create New</a>
</p>
<div>
<QuickGrid Class="table" Items="FilteredMovies" Pagination="pagination">
    <PropertyColumn Property="movie => movie.Title" Sortable="true" />
    <PropertyColumn Property="movie => movie.ReleaseDate" Title="Release Date" />
    <PropertyColumn Property="movie => movie.Genre" />
    <PropertyColumn Property="movie => movie.Price" />
    <PropertyColumn Property="movie => movie.Rating" />

    <TemplateColumn Context="movie">
        <a href="@($"movies/edit?id={movie.Id}")">Edit</a> |
        <a href="@($"movies/details?id={movie.Id}")">Details</a> |
        <a href="@($"movies/delete?id={movie.Id}")">Delete</a>
    </TemplateColumn>
</QuickGrid>
<Paginator State="pagination" />
</div>
@code {
    //分页
    private PaginationState pagination = new PaginationState { ItemsPerPage = 5 };

    //搜索
    // [SupplyParameterFromQuery]
    // private string? TitleFilter { get; set; }

    // private IQueryable<Movie> FilteredMovies =>
    // context.Movie.Where(m => m.Title!.Contains(TitleFilter ?? string.Empty));

    //交互式搜索
    private string titleFilter = string.Empty;

    private IQueryable<Movie> FilteredMovies =>
        context.Movie.Where(m => m.Title!.Contains(titleFilter));


    private BlazorWebAppMoviesContext context = default!;

    protected override void OnInitialized()
    {
        context = DbFactory.CreateDbContext();
    }

    public async ValueTask DisposeAsync() => await context.DisposeAsync();
}
